package com.cn.yz.service.sysauthority.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cn.yz.common.authority.AcceptToken;
import com.cn.yz.common.httpentity.ResponseObject;
import com.cn.yz.common.httpentity.ResponseString;
import com.cn.yz.common.utils.EncryptUtil;
import com.cn.yz.service.sysauthority.model.SysUser;
import com.cn.yz.service.sysauthority.service.LoginService;
import com.cn.yz.service.sysauthority.vo.LoginVO;

@RestController()
@RequestMapping("/main")
public class LoginController {
	
	private static Logger LOG = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private LoginService loginService;

	/**
	 * 登录
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
	public ResponseString login(@RequestBody LoginVO loginVO,HttpServletResponse response){
		LOG.info("LoginController.login");
		
		SysUser sysUser = new SysUser();
		
		String accountEncrypt = loginVO.getAccount();
		String account = EncryptUtil.decryptBase64(accountEncrypt);
		sysUser.setAccount(account);
		
		String passwordEncrypt = loginVO.getPassword();
		String password = EncryptUtil.decryptBase64(passwordEncrypt);
		sysUser.setPassword(password);
		loginService.login(sysUser, response);
		return new ResponseString();
	}
	
	/**
	 * 根据id修改密码
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/modPassword", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
	public ResponseString modPassword(@RequestBody SysUser sysUser){
		
		LOG.info("LoginController.modPassword");
		String accountEncrypt = sysUser.getAccount();
		String account = EncryptUtil.decryptBase64(accountEncrypt);
		sysUser.setAccount(account);
		
		loginService.modPassword(sysUser);
		return new ResponseString();
	}
	
	/**
	 * 登出
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
	public ResponseString logout(@RequestBody SysUser sysUser){
		LOG.info("LoginController.logout");
		loginService.logout(sysUser.getAccount());
		return new ResponseString();
	}
	
	/**
	 * 根据账号修改密码(忘记密码)
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/modPasswordByAccount", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
	public ResponseString modPasswordByAccount(@RequestBody SysUser sysUser){
		LOG.info("LoginController.modPasswordByAccount");
		
		String accountEncrypt = sysUser.getAccount();
		String account = EncryptUtil.decryptBase64(accountEncrypt);
		sysUser.setAccount(account);
		
		String passwordEncrypt = sysUser.getPassword();
		String password = EncryptUtil.decryptBase64(passwordEncrypt);
		sysUser.setPassword(password);
		
		loginService.modPasswordByAccount(sysUser);
		return new ResponseString();
	}
}
